import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONUtil;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class AESUtil {  
    private static final String ALGORITHM = "AES";  
    private static final int KEY_SIZE = 128; // 可以是128, 192, 或 256  
  
    // 生成AES密钥  
    public static SecretKey generateKey() throws Exception {  
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);  
        keyGenerator.init(KEY_SIZE, new SecureRandom());  
        return keyGenerator.generateKey();  
    }  
  
    // 加密  
    public static String encrypt(String content, String secretKey, String random) {
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            e.printStackTrace();
        }
        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(random.getBytes(),0 ,16);
        try {
            assert cipher != null;
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
        } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        byte[] encryptedBytes = new byte[0];
        try {
            encryptedBytes = cipher.doFinal(content.getBytes());
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }

        return Base64.encodeUrlSafe(encryptedBytes);
    }  
  
    // 解密  
    public static String decrypt(String encryptedContent, String secretKey, String random) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(random.getBytes(),0 ,16);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
        byte[] ddd = Base64.decode(encryptedContent);
        byte[] originalBytes = cipher.doFinal(ddd);
        return new String(originalBytes);  
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> dd= new HashMap<>();
        dd.put("id", 15L);
        dd.put("memberId", 1L);
        String content = encrypt(JSONUtil.toJsonStr(dd), "LZXSKZK9jSw1owWLPnljFTRNrqx2La2s", "NHQxz6cEHIRv39yn");
        System.out.println(content);
        System.out.println(decrypt(content, "LZXSKZK9jSw1owWLPnljFTRNrqx2La2s", "NHQxz6cEHIRv39yn"));


//        String iii = cn.hutool.core.codec.Base64.encode();
//        System.out.println(iii);
//        System.out.println(new String(cn.hutool.core.codec.Base64.decode(iii)));
    }
}